#include "los_queue.h"
#include "stdio.h"
#include <stdint.h>
#include "ohos_init.h"
#include "los_task.h"
#include "cmsis_os2.h"
static UINT32 g_queue;
#define BUFFER_LEN 50
/***************************************************************
 * 函数名称: SendEntry
 * 说    明: 线程发送信息函数
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void SendEntry(void)
{
    UINT32 ret = 0;
    CHAR abuf[30] = "SendEntry message";
    UINT32 len = sizeof(abuf);
    while(1){
        /*发送测试信息到信息队列*/
        printf("<<==>>%s\n",abuf);
        len = sizeof(abuf);
        ret = LOS_QueueWriteCopy(g_queue, abuf, len, LOS_WAIT_FOREVER);
        if(ret != LOS_OK) {
            printf("<<==>>send message failure, error: %x\n", ret);
        }
        
        /* 任务休眠5s */
        LOS_Msleep(1000); 
    }
}
/***************************************************************
 * 函数名称: RecvEntry
 * 说    明: 线程接收信息函数1
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void RecvEntry(void)
{
    UINT32 ret = 0;
    CHAR readBuf[BUFFER_LEN] = {0};
    UINT32 readLen = BUFFER_LEN;
    while(1){
        /*接收测试信息从信息队列*/
        ret = LOS_QueueReadCopy(g_queue, readBuf, &readLen, LOS_WAIT_FOREVER);
        if(ret == LOS_OK) {
            printf("<<==>>RecvEntry message: %s\n", readBuf);
        }
        /* 任务休眠1s */
        //LOS_Msleep(1000); 
    }

}   
/***************************************************************
 * 函数名称: RecvEntry2
 * 说    明: 线程接收信息函数2
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void RecvEntry2(void)
{
    UINT32 ret = 0;
    CHAR readBuf[BUFFER_LEN] = {0};
    UINT32 readLen = BUFFER_LEN;
    while(1){
        /*接收测试信息从信息队列*/
        ret = LOS_QueueReadCopy(g_queue, readBuf, &readLen, LOS_WAIT_FOREVER);
        if(ret == LOS_OK) {
            printf("<<==>>RecvEntry2 message: %s\n", readBuf);
            ret = LOS_QueueDelete(g_queue);
            printf("<<==>>RecvEntry2 Del: %d\n", ret);
        }
        
    }

}   
/***************************************************************
 * 函数名称: ExampleQueue
 * 说    明: 创建对应线程任务
 * 参    数: 无
 * 返 回 值: 返回创建状态是否成功
 ***************************************************************/
UINT32 ExampleQueue(void)
{
    printf("<<==>>start queue example\n");
    UINT32 ret = 0;
    UINT32 task1, task2, task3;
    TSK_INIT_PARAM_S initParam = {0};
    TSK_INIT_PARAM_S initParam1 = {0};
    TSK_INIT_PARAM_S initParam2 = {0};
    /*创建信息队列*/
    ret = LOS_QueueCreate("<<==>>queue", 5, &g_queue, 0, 100);
    if(ret != LOS_OK) {
        printf("<<==>>create queue failure, error: %x\n", ret);
    }
    printf("<<==>>create the queue success!\n");
    /*创建发送信息任务*/
    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)SendEntry;
    initParam.usTaskPrio = 11;
    initParam.uwStackSize = 4096;
    initParam.pcName = "SendQueue";
    ret = LOS_TaskCreate(&task1, &initParam);
    if(ret != LOS_OK) {
        printf("<<==>>create task1 failed, error: %x\n", ret);
        return ret;
    }
    /*创建接收信息任务*/
    initParam1.pcName = "RecvQueue";
    initParam1.usTaskPrio = 10;
    initParam1.uwStackSize = 4096;
    initParam1.pfnTaskEntry = (TSK_ENTRY_FUNC)RecvEntry;
    ret = LOS_TaskCreate(&task2, &initParam1);
    if(ret != LOS_OK) {
        printf("<<==>>create task2 failed, error: %x\n", ret);
        return ret;
    }
    /* 任务休眠5s 
    LOS_Msleep(5000); */
    /*创建接收信息任务2
    initParam2.pcName = "RecvQueue2";
    initParam2.usTaskPrio = 9;
    initParam2.uwStackSize = 4096;
    initParam2.pfnTaskEntry = (TSK_ENTRY_FUNC)RecvEntry2;
    ret = LOS_TaskCreate(&task3, &initParam2);
    if(ret != LOS_OK) {
        printf("<<==>>create task3 failed, error: %x\n", ret);
        return ret;
    }*/
    return ret;
}